package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xpvideo.entity.LsCustom;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.service.FileService;
import com.example.xpvideo.service.LsCustomService;
import com.example.xpvideo.mapper.LsCustomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 *
 */
@Service
public class LsCustomServiceImpl extends ServiceImpl<LsCustomMapper, LsCustom>
    implements LsCustomService{

    @Autowired
    private LsCustomMapper customMapper;
    @Autowired
    private FileService fileService;

    @Override
    public ResponseResult updateCustom(Map<String, String> params) {
        String id = params.get("id");
        String name = params.get("name");
        if (null == id || "".equals(id)) {
            return ResponseResult.responseResult(ErrCodeEnum.unknown_10001, "id不能为空");
        } else  if (null == name || "".equals(name)){
            return ResponseResult.responseResult(ErrCodeEnum.unknown_10001, "id不能为空");
        }
        LsCustom custom = new LsCustom();
        custom.setId(Integer.parseInt(id));
        custom.setName(name);
        int i = customMapper.updateById(custom);
        return i > 0 ? ResponseResult.responseResult(0) : ResponseResult.responseResult(ErrCodeEnum.unknown_20000, null);
    }


    @Override
    public ResponseResult addCombo(LsCustom custom, MultipartFile file) {
        Integer parent_id = customMapper.selectCount(new QueryWrapper<LsCustom>().eq("parent_id", custom.getParentId()));
        if (null != parent_id && parent_id == 10) {
            // 自定义专区限制 只能添加十个套餐商品,如果已经十个就不能添加
            return ResponseResult.responseResult(ErrCodeEnum.unknown_20002, "添加已上限!");
        }
        if (null == file) {
            return ResponseResult.responseResult(500, "请上传图片");
        }
        String imagesUrl = fileService.addImages(file);
        custom.setImages(imagesUrl);
        int insert = customMapper.insert(custom);
        return insert > 0 ? ResponseResult.responseResult(0) : ResponseResult.responseResult(ErrCodeEnum.unknown_20002, null);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    public ResponseResult delCombo(Integer id) {
        LsCustom custom = customMapper.selectById(id);
        fileService.delImages(custom.getImages());
        int i = customMapper.deleteById(id);
        return i > 0 ? ResponseResult.responseResult(0) : ResponseResult.responseResult(ErrCodeEnum.unknown_20000, null);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    public ResponseResult editCombo(LsCustom custom, MultipartFile file) {
        if (null==custom.getId()) {
            return ResponseResult.responseResult(ErrCodeEnum.unknown_10001, "请选择编辑内容");
        }
        if (null != file) {
            // 需要修改套餐图片
            // 删除原图 在添加新图
            LsCustom custom1 = customMapper.selectById(custom.getId());
            if (null != custom1) {
                fileService.delImages(custom1.getImages());
            }
            String imagesUrl = fileService.addImages(file);
            custom.setImages(imagesUrl);
            int insert = customMapper.updateById(custom);
            return insert > 0 ? ResponseResult.responseResult(0) : ResponseResult.responseResult(ErrCodeEnum.unknown_20000, null);
        } else {
            // 无需修改图片
            int insert = customMapper.updateById(custom);
            return insert > 0 ? ResponseResult.responseResult(0) : ResponseResult.responseResult(ErrCodeEnum.unknown_20000, null);
        }
    }
}




